package com.lzb.findphone.fragment;

import android.media.AudioManager;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;

import com.blankj.utilcode.util.VolumeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lzb.findphone.BaseNoModelFragment;
import com.lzb.findphone.R;
import com.lzb.findphone.adapter.SoundDurationAdapter;
import com.lzb.findphone.bean.DurationBean;
import com.lzb.findphone.bean.SettingBean;
import com.lzb.findphone.databinding.FragmentSoundBinding;
import com.lzb.findphone.utils.AppSpUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link SoundFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class SoundFragment extends BaseNoModelFragment<FragmentSoundBinding> implements View.OnClickListener, OnItemClickListener {

    private SoundDurationAdapter durationAdapter;
    private List<DurationBean> listShow = new ArrayList<>();
    private int oldPosition;
    private int max, min, now, progress;

    @Override
    protected int onCreate() {
        return R.layout.fragment_sound;
    }

    @Override
    protected void initView() {
        mDataBinding.ivMin.setOnClickListener(this);
        mDataBinding.ivAdd.setOnClickListener(this);

        mDataBinding.rvDurationList.setLayoutManager(new LinearLayoutManager(mContext));
        durationAdapter = new SoundDurationAdapter();
        mDataBinding.rvDurationList.setAdapter(durationAdapter);
        durationAdapter.setOnItemClickListener(this);

        max = VolumeUtils.getMaxVolume(AudioManager.STREAM_MUSIC);
        min = VolumeUtils.getMinVolume(AudioManager.STREAM_MUSIC);
        now = VolumeUtils.getVolume(AudioManager.STREAM_MUSIC);
        progress = now * 100 / (max - min);
        mDataBinding.tvProgress.setText(progress + getString(R.string.unit_percent));
        mDataBinding.sbProgress.setProgress(progress);
        mDataBinding.sbProgress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                progress = i;
                setVolume();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.ivMin) {//音量-
            if (progress == 0) return;
            progress--;
            setVolume();
        } else if (id == R.id.ivAdd) {//音量+
            if (progress == 100) return;
            progress++;
            setVolume();
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        durationAdapter.getItem(oldPosition).setSelected(false);
        oldPosition = position;
        durationAdapter.getItem(position).setSelected(true);
        durationAdapter.notifyDataSetChanged();
        SettingBean settingBean = AppSpUtils.getSetting();
        settingBean.setDuration(durationAdapter.getItem(position).getDuration());
        AppSpUtils.saveSetting(settingBean);
    }

    @Override
    protected void initData() {
        String[] durationArr = getResources().getStringArray(R.array.sound_duration_data);
        listShow.add(new DurationBean(durationArr[0], 5 * 1000L, false));
        listShow.add(new DurationBean(durationArr[1], 10 * 1000L, false));
        listShow.add(new DurationBean(durationArr[2], 30 * 1000L, false));
        listShow.add(new DurationBean(durationArr[3], 90 * 1000L, false));
        listShow.add(new DurationBean(durationArr[4], 0, false));

        SettingBean settingBean = AppSpUtils.getSetting();
        for (int i = 0; i < listShow.size(); i++) {
            DurationBean durationBean = listShow.get(i);
            if (settingBean.getDuration() == durationBean.getDuration()) {
                oldPosition = i;
                listShow.get(i).setSelected(true);
                break;
            }
        }
        durationAdapter.setList(listShow);

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            SettingBean settingBean = AppSpUtils.getSetting();
            for (int i = 0; i < listShow.size(); i++) {
                DurationBean durationBean = listShow.get(i);
                if (settingBean.getDuration() == durationBean.getDuration()) {
                    listShow.get(oldPosition).setSelected(false);
                    oldPosition = i;
                    listShow.get(i).setSelected(true);
                    break;
                }
            }
            durationAdapter.setList(listShow);
        }
    }

    private void setVolume() {
        mDataBinding.tvProgress.setText(progress + getString(R.string.unit_percent));
        float value = calSeekBarRealValue(mDataBinding.sbProgress, progress);
        VolumeUtils.setVolume(AudioManager.STREAM_MUSIC, (int) value, 0);
    }

    private float calSeekBarRealValue(SeekBar seekBar, int progress) {
        int progressRange = seekBar.getMax();
        float value = progress * 1.0f / progressRange * (max - min) + min;
        return value;
    }

}